package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectAllClazzDTO;
import com.itheima.tlias84.dto.SelectClassByIdDTO;
import com.itheima.tlias84.po.InsertClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.service.ClazzService;
import com.itheima.tlias84.service.impl.ClazzServiceImpl;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Resource
    ClazzService clazzService;

    @GetMapping
    public ResultUtil selectClazzByPage(SelectClazzPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        return clazzService.selectClazzByPage(param);
    }
    @GetMapping("/list")
    public ResultUtil selectAllClazz(){
        List<SelectAllClazzDTO> selectAllClazzDTOS = clazzService.selectAllClazz();
        return ResultUtil.success(selectAllClazzDTOS);
    }
    @GetMapping("/{id}")
    public ResultUtil selectClazzById(@PathVariable("id") Integer id){
        SelectClassByIdDTO selectClassByIdDTO = clazzService.selectClazzById(id);
        return ResultUtil.success(selectClassByIdDTO);
    }

    @PostMapping
    public ResultUtil insertOneClazz(@RequestBody InsertClazzParam param){
        int i = clazzService.insertOneClazz(param);
        return i==0?ResultUtil.fail("新增失败"):ResultUtil.success("新增成功");
    }

    @PutMapping
    public ResultUtil updateClazz(@RequestBody UpdateClazzParam param){
        int i = clazzService.updateClazz(param);
        return i==0?ResultUtil.fail("更新失败"):ResultUtil.success("更新成功");
    }
    @DeleteMapping("/{id}")
    public ResultUtil deleteBatch(@PathVariable Integer id){
        if (Objects.isNull(id)){
            return ResultUtil.fail("请先选择的id");
        }
        int deletebatch = clazzService.deletebatch(id);
        return ResultUtil.success(deletebatch);
    }
}
